﻿
#include "complextablewidget.h"
#include "ui_complextablewidget.h"
#include<QPushButton>
#include<QSplitter>
#include<QLabel>
#include<QDebug>
#include "schemedialog.h"
#include "infos.h"
#include<QDebug>
ComplexTableWidget::ComplexTableWidget(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::ComplexTableWidget)
{
    ui->setupUi(this);
    initUI();
}

ComplexTableWidget::~ComplexTableWidget()
{
    delete ui;
}

void ComplexTableWidget::initUI()
{
    setWindowFlags(Qt::Dialog|Qt::WindowMaximizeButtonHint|Qt::WindowCloseButtonHint);
    ui->searchLEdit->setPlaceholderText("请输入名称");
    ui->tableWidget->horizontalHeader()->setVisible(false);
    ui->tableWidget->horizontalHeader()->setSelectionBehavior(QAbstractItemView::SelectRows);   // 选择行为：行
    ui->tableWidget->horizontalHeader()->setSelectionMode(QAbstractItemView::SingleSelection);  // 选择模式：单选
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    ui->tableWidget->setColumnCount(1);
    ui->tableWidget->setRowCount(0);
    m_schemeItems.clear();
    QMap<QString,QList<FATable>> nameIdMap;
    for (auto &it:faTable) {
        nameIdMap[it.nameId].append(it);
    }
    QMap<QString,QString> idToNameMap;
    for (auto &it:nTable) {
        idToNameMap.insert(it.nameId,it.name);
    }

    for (auto &namekey:nameIdMap.keys())
    {
        m_schemeItems.append(idToNameMap.value(namekey));
        int row=ui->tableWidget->rowCount();

        ui->tableWidget->insertRow(row);

        auto *schemeDialog=new SchemeDialog(namekey, nameIdMap.value(namekey));
        // 将父级对话框设置为单元格的组件
        ui->tableWidget->setCellWidget(row, 0, schemeDialog);
        // 设置单元格的行高以适应内容
        ui->tableWidget->resizeRowsToContents();

        connect(schemeDialog, &SchemeDialog::isFoldSignal, [this]() {
            qDebug()<<"点击";
            // 设置单元格的行高以适应内容
            ui->tableWidget->resizeRowsToContents();
        });
    }

}

void ComplexTableWidget::on_searchBtn_clicked()
{
    QString search = ui->searchLEdit->text();
    if(search.isEmpty())
        return;
    int row = m_schemeItems.indexOf(search);
    if(row==-1)
        return;

    auto item=static_cast<SchemeDialog*>(ui->tableWidget->cellWidget(row,0));
    item->setFoldView(false);
    ui->tableWidget->setCurrentCell(row,0);
    // 设置单元格的行高以适应内容
    ui->tableWidget->resizeRowsToContents();
}

